package com.py.androidUtils.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.py.androidUtils.utils.TextDrawableUnits;
import com.readyidu.app.R;

/**
 * @说明 topView
 * @作者 LY
 * @文件 TitleBarView.java
 * @时间 2015年6月23日 上午9:02:22
 * @版权 Copyright(c) 2014 LY-版权所有
 */
public class TitleBarView {
	private Window window;// window实例
	private RelativeLayout titleBar;// titleBar
	private ImageView imgLeft;
	private TextView tvLeft;
	private TextView tvTitle;
	private ImageView imgRight1;
	private ImageView imgRight2;
	private TextView tvRight1;
	private TextView tvRight2;

	public TitleBarView(Window window) {
		this.window = window;
	}

	public RelativeLayout getTitleBar() {
		if (titleBar == null && window != null) {
			titleBar = (RelativeLayout) window.findViewById(R.id.titleBar);
		}
		return titleBar;
	}

	public ImageView getImgLeft() {
		if (imgLeft == null && window != null) {
			imgLeft = (ImageView) window.findViewById(R.id.imgLeft);
		}
		return imgLeft;
	}

	public TextView getTvLeft() {
		if (tvLeft == null && window != null) {
			tvLeft = (TextView) window.findViewById(R.id.tvLeft);
		}
		return tvLeft;
	}

	public TextView getTvTitle() {
		if (tvTitle == null && window != null) {
			tvTitle = (TextView) window.findViewById(R.id.tvTitle);
		}
		return tvTitle;
	}

	public ImageView getImgRight1() {
		if (imgRight1 == null && window != null) {
			imgRight1 = (ImageView) window.findViewById(R.id.imgRight1);
		}
		return imgRight1;
	}

	public ImageView getImgRight2() {
		if (imgRight2 == null && window != null) {
			imgRight2 = (ImageView) window.findViewById(R.id.imgRight2);
		}
		return imgRight2;
	}

	public TextView getTvRight1() {
		if (tvRight1 == null && window != null) {
			tvRight1 = (TextView) window.findViewById(R.id.tvRight1);
		}
		return tvRight1;
	}

	public TextView getTvRight2() {
		if (tvRight2 == null && window != null) {
			tvRight2 = (TextView) window.findViewById(R.id.tvRight2);
		}
		return tvRight2;
	}

	/**
	 * 设置标题名称
	 * 
	 * @param title名称
	 */
	public void setTitleBar(CharSequence title) {
		getTvTitle().setText(title);
	}

	/**
	 * 设置标题名称
	 * 
	 * @param title名称
	 */
	public void setTitleBar(int title) {
		getTvTitle().setText(title);
	}

	/**
	 * 设置左边图标和事件
	 * 
	 * @param resId图标
	 * @param clickListener事件
	 */
	public void setImgLeft(int resId, OnClickListener clickListener) {
		getImgLeft().setVisibility(View.VISIBLE);
		getImgLeft().setImageResource(resId);
		if (clickListener != null) {
			getImgLeft().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置左边图标和事件
	 * 
	 * @param resId图标
	 * @param clickListener事件
	 */
	public void setImgLeft(Drawable drawable, OnClickListener clickListener) {
		getImgLeft().setImageDrawable(drawable);
		getImgLeft().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getImgLeft().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置左边文字按钮
	 * 
	 * @param title文字
	 * @param clickListener事件
	 */
	public void setTvLeft(CharSequence title, OnClickListener clickListener) {
		getTvLeft().setText(title);
		getTvLeft().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getTvLeft().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置左边文字按钮
	 * 
	 * @param title文字
	 * @param clickListener事件
	 */
	public void setTvLeft(int title, OnClickListener clickListener) {
		getTvLeft().setText(title);
		getTvLeft().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getTvLeft().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置左边文字按钮带图标
	 * 
	 * @param context
	 * @param title文字
	 * @param leftResId图标
	 * @param clickListener事件
	 */
	public void setTvLeft(Context context, int title, int leftResId, OnClickListener clickListener) {
		getTvLeft().setText(title);
		getTvLeft().setVisibility(View.VISIBLE);
		TextDrawableUnits.setDrawableLeft(context, getTvLeft(), leftResId);
		if (clickListener != null) {
			getTvLeft().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边第一个图标和事件
	 * 
	 * @param resId图标
	 * @param clickListener事件
	 */
	public void setImgRight1(int resId, OnClickListener clickListener) {
		getImgRight1().setVisibility(View.VISIBLE);
		getImgRight1().setImageResource(resId);
		if (clickListener != null) {
			getImgRight1().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边第一个图标和事件
	 * 
	 * @param resId图标
	 * @param clickListener事件
	 */
	public void setImgRight1(Drawable drawable, OnClickListener clickListener) {
		getImgRight1().setImageDrawable(drawable);
		getImgRight1().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getImgRight1().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边第二个图标和事件
	 * 
	 * @param resId图标
	 * @param clickListener事件
	 */
	public void setImgRight2(int resId, OnClickListener clickListener) {
		getImgRight2().setVisibility(View.VISIBLE);
		getImgRight2().setImageResource(resId);
		if (clickListener != null) {
			getImgRight2().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边第二个图标和事件
	 * 
	 * @param resId图标
	 * @param clickListener事件
	 */
	public void setImgRight2(Drawable drawable, OnClickListener clickListener) {
		getImgRight2().setImageDrawable(drawable);
		getImgRight2().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getImgRight2().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边文字按钮
	 * 
	 * @param title文字
	 * @param clickListener事件
	 */
	public void setTvRight1(CharSequence title, OnClickListener clickListener) {
		getTvRight1().setText(title);
		getTvRight1().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getTvRight1().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边文字按钮
	 * 
	 * @param title文字
	 * @param clickListener事件
	 */
	public void setTvRight1(int title, OnClickListener clickListener) {
		getTvRight1().setText(title);
		getTvRight1().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getTvRight1().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边文字按钮
	 * 
	 * @param title文字
	 * @param clickListener事件
	 */
	public void setTvRight2(CharSequence title, OnClickListener clickListener) {
		getTvRight2().setText(title);
		getTvRight2().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getTvRight2().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置右边文字按钮
	 * 
	 * @param title文字
	 * @param clickListener事件
	 */
	public void setTvRight2(int title, OnClickListener clickListener) {
		getTvRight2().setText(title);
		getTvRight2().setVisibility(View.VISIBLE);
		if (clickListener != null) {
			getTvRight2().setOnClickListener(clickListener);
		}
	}

	/**
	 * 设置titleBar颜色
	 * 
	 * @param color
	 */
	public void setTitleBarColorBg(int color) {
		getTitleBar().setBackgroundColor(color);
	}

	/**
	 * 设置titleBar背景图片
	 * 
	 * @param resId
	 */
	public void setTitleBarImagesBg(int resId) {
		getTitleBar().setBackgroundResource(resId);
	}

}
